#ifndef RTTIMERPRECISEWRAPPERID_H
#define RTTIMERPRECISEWRAPPERID_H

#include "RtBase.h"
#include "RtReactorInterface.h"

class CRtTimerWrapper;
class CRtTimerQueueCalendar;
class CRtTimerPreciseWrapperID;


class /*RT_API_EXPORT*/ CRtTimerPreciseWrapperIDSink
{
public:
	virtual void OnTimer(CRtTimerPreciseWrapperID* aId) = 0;

protected:
	virtual ~CRtTimerPreciseWrapperIDSink(){};
};

/********************************************************
	For 10ms high resolution timer.
 ********************************************************/
class /*RT_API_EXPORT*/ CRtTimerPreciseManager
: public CRtThread
, public CRtStopFlag
{
private:
	CRtTimerPreciseManager();

public:
	virtual ~CRtTimerPreciseManager();
	
	static CRtTimerPreciseManager* Instance();

	//Implement interface CRtThread.
	virtual void OnThreadInit();
	virtual void OnThreadRun();
	virtual RtResult Stop(CRtTimeValue* aTimeout = NULL);

	RtResult Schedule(
		CRtTimerPreciseWrapperID* aTimerId,
		CRtTimerPreciseWrapperIDSink* aSink,
		const CRtTimeValue &aInterval,
		DWORD aCount = 0);
	RtResult Cancel(CRtTimerPreciseWrapperID* aTimerId);
	
	
private:

	CRtTimerQueueCalendar* m_pTimerQueue;
	WORD m_wTimerRes; //timer resolution
	WORD m_wTimerId;  //timer ID

	CRtMutexThread m_Mutex;
};

/********************************************************
	CRtTimerPreciseWrapperID,10ms high resolution timer.
 ********************************************************/
class /*RT_API_EXPORT*/ CRtTimerPreciseWrapperID
: public IRtTimerHandler
{
public:
	CRtTimerPreciseWrapperID();
	virtual ~CRtTimerPreciseWrapperID();

	/// Schedule an timer that will expire after <aInterval> for <aCount> times. 
	/// if <aCount> is 0, schedule infinite times.
	RtResult Schedule( CRtTimerPreciseWrapperIDSink* aSink,
		const CRtTimeValue &aInterval,
		DWORD aCount = 0,
		CRtThreadManager::TType aTType = CRtThreadManager::TT_MAIN);

	RtResult Cancel();

	//Implement interface IRtTimerHandler
	virtual void OnTimeout(const CRtTimeValue &aCurTime, LPVOID aArg);

public:
	BOOL GetStatus(){ return m_bScheduled; }

private:
	BOOL m_bScheduled;
	CRtThreadManager::TType m_TType;
	CRtThread* m_pThread; //for OnTimeOut callback.
	IRtEventQueue* m_pEventQueue;
};

/********************************************************
	For CEventTimer, by now. for CRtTimerPreciseWrapperID.
 ********************************************************/
class CEventTimer : public IRtEvent
{
public:
	CEventTimer(CRtTimerPreciseWrapperIDSink *pSink,CRtTimerPreciseWrapperID* pTimerId);
	virtual ~CEventTimer();
	
	//Implement IRtEvent;
	virtual RtResult OnEventFire();
	
private:
	CRtTimerPreciseWrapperIDSink* m_pSink;
	CRtTimerPreciseWrapperID* m_pTimerId;
};


#endif // !RTTIMERPRECISEWRAPPERID_H
